<template>
	<view class='container'>
	  <!-- <view class='line-fix'></view>
	  <view class='tit-background'></view> -->
	  <view>
		<block v-for="(item,index) in prodList" :key ="index">
		  <prod :item="item" ></prod>
		</block>
	  </view>
	  <view v-if="!prodList.length" class="empty-list">
		暂无商品
	  </view>
	</view>
</template>

<script>
	import { request } from '../../utils/http.js'
	import { prod } from '../../components/production/production.vue'
	export default {
		data() {
			return {
				sts: 0,
				prodList: [],
				title: "",
				current: 1,
				size: 10,
				pages: 0,
				tagid: 0
			}
		},
		onLoad(options){
				console.log(options,'路由传参')
			    this.current=1,
			    this.pages=0,
			    this.sts=options.sts,
			    this.title=options.title ? options.title : ""
			    if (options.tagid) {
			        this.tagid=options.tagid
			    }
			
			    if (this.sts == 0) {
			      if (options.tagid == 1) {
			        uni.setNavigationBarTitle({
			          title: '每日上新'
			        })
			      } else if (options.tagid == 2) {
			        uni.setNavigationBarTitle({
			          title: '商城热卖'
			        })
			      } else if (options.tagid == 3) {
			        uni.setNavigationBarTitle({
			          title: '更多宝贝'
			        })
			      }
			    } else if (this.sts == 1) {
			      uni.setNavigationBarTitle({
			        title: '新品推荐',
			      })
			    } else if (this.sts == 2) {
			      uni.setNavigationBarTitle({
			        title: '限时特惠',
			      })
			    } else if (this.sts == 3) {
			      uni.setNavigationBarTitle({
			        title: '每日疯抢',
			      })
			    } else if (this.sts == 4) {
			      uni.setNavigationBarTitle({
			        title: '优惠券活动商品',
			      })
			    } else if (this.sts == 5) {
			      uni.setNavigationBarTitle({
			        title: '我的收藏商品',
			      })
			    } else {
			      uni.setNavigationBarTitle({
			        title: this.title
			      })
			    }
			    this.loadProdData(options);
		},
		/**
		   * 页面上拉触底事件的处理函数
		   */
	    onReachBottom: function(e){
		
			if (e.current < e.pages) {
			  
			  e.current=e.current + 1
			  e.loadProdData()
			}
	    },
		methods: {
			loadProdData: function(options) {//加载商品数据
			    let sts = this.sts
			    if (sts == 0) {
			      // 分组标签商品列表
			      this.getTagProd();
			    } else if (sts == 1) {
			      // 新品推荐
			      let url = "/prod/lastedProdPage"
			      this.getActProd(url)
			    } else if (sts == 2) {
			      // 限时特惠
			      let url = "/prod/discountProdList"
			      this.getActProd(url)
			    } else if (sts == 3) {
			      // 每日疯抢
			      let url = "/prod/moreBuyProdList"
			      this.getActProd(url)
			    } else if (sts == 4) {
			      // 优惠券商品列表
			      this.getProdByCouponId(options.tagid)
			    } else if (sts == 5) {
			      // 收藏商品列表
			      this.getCollectionProd()
			    }
			},
			getTagProd: function(id) {//获取标签列表
			    let ths = this;
			    uni.showLoading();
			    var param = {
			      url: "/prod/prodListByTagId",
			      method: "GET",
			      data: {
			        tagId: ths.tagid,
			        current: ths.current,
			        size: ths.size
			      },
			      callBack: (res) => {
			        let list = []
			        if (res.current == 1) {
			          list = res.records
			        } else {
			          list = ths.prodList.concat(res.records)
			        }
			        
			          ths.prodList=list;
			          ths.pages=res.pages;
			        
			        uni.hideLoading();
			      }
			    };
			    request(param);
			  },
			getActProd: function(url) {//新品推荐
			    var ths = this;
			    uni.showLoading();
			    var params = {
			      url: url,
			      method: "GET",
			      data: {
			        current: ths.current,
			        size: ths.size,
			      },
			      callBack: function(res) {
			        let list = []
			        if (res.current == 1) {
			          list = res.records
			        } else {
			          list = ths.data.prodList
			          list = list.concat(res.records)
			        }
			        
			        ths.prodList=list;
			        ths.pages=res.pages
			        uni.hideLoading();
			      }
			    };
			    request(params);
			},
			getProdByCouponId(id) {//获取优惠券商品列表
			    var ths = this;
			    uni.showLoading();
			    var param = {
			      url: "/coupon/prodListByCouponId",
			      method: "GET",
			      data: {
			        couponId: id,
			        current: this.current,
			        size: this.size
			      },
			      callBack: (res) => {
			        let list = []
			        if (res.current == 1) {
			          list = res.records
			        } else {
			          list = ths.prodList.concat(res.records)
			        }

			          ths.prodList=list
			          ths.pages=res.pages
			        uni.hideLoading();
			      }
			    };
			    request(param);
			},
			getCollectionProd: function() {//获取我的收藏商品
			    var ths = this;
			    uni.showLoading();
			    var params = {
			      url: "/p/user/collection/prods",
			      method: "GET",
			      data: {
			        current: this.current,
			        size: this.size,
			      },
			      callBack: (res)=>{
			
			        let list = []
			        if (res.current == 1) {
			          list = res.records
			        } else {
			          list = ths.prodList
			          list = list.concat(res.records)
			        }

			          this.prodList=list;
			          this.pages=res.pages
			        uni.hideLoading();
			      }
			    };
			    request(params);
			}
		},
		components:{
			prod
		}
	}
</script>

<style>
/* pages/prod-classify/prod-classify.wxss */

page {
  background: #fff;
}

.line-fix {
  width: 100%;
  height: 2rpx;
  background: #e1e1e1;
  position: fixed;
  top: 0;
}

.tit-background {
  width: 100%;
  height: 20rpx;
  background: #f4f4f4;
}

.empty-list {
  font-size: 24rpx;
  color: #999;
  text-align: center;
  margin: 100rpx 0;
}

</style>
